package com.iwhalecloud.bss.kite.cucc.common.util;

/**
 * Classname     :  XMLTools
 * Description   :  XML文件生成器
 * @Author       :  arthur xu
 * Data          :  2007-03-08
 */

/*import org.jdom.output.XMLOutputter;
import org.jdom.Document;
import org.jdom.Element;*/

import org.jdom2.Attribute;
import org.jdom2.CDATA;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;

public class XMLLinkedTools {
    XMLOutputter outputter;

    Document doc;

    public XMLLinkedTools() {
        outputter = new XMLOutputter();
    }

    public void execute(XMLLinkedObject object) {
        Element root = new Element(object.getName());
        doc = new Document(root);

        if (object.getHmAttrib() != null) {
            LinkedHashMap hmAttrib = object.getHmAttrib();
            setAttrib(root, hmAttrib);
        }

        LinkedHashMap props = object.getProds();
        Set key = props.keySet();
        Iterator it = key.iterator();
        while (it.hasNext()) {
            String name = (String) it.next();
            if (props.get(name) instanceof LinkedHashMap) {
                Element child = new Element(name);
                setChildElement(root, child, (LinkedHashMap) props.get(name));
            }
            else if (props.get(name) instanceof String) {
                setChildElement(root, name, (String) props.get(name));
            }
            else if (props.get(name) instanceof CDATA) {
                setChildElement(root, name, (CDATA) props.get(name));
            }
            else if (props.get(name) instanceof ArrayList) {
                setChildElement(root, name, (ArrayList) props.get(name));
            }
        }
    }

    public Document getDoc() {
        return doc;
    }

    public String getDocString() throws IOException {
        if (doc != null) {
            return outputter.outputString(doc);
        }
        else {
            return "";
        }
    }

    private void setAttrib(Element em, LinkedHashMap hmAttrib) {
        Set key = hmAttrib.keySet();
        Iterator it = key.iterator();

        while (it.hasNext()) {
            String keyName = (String) it.next();
            em.setAttribute(new Attribute(keyName, (String) hmAttrib.get(keyName)));
        }
    }

    private void setChildElement(Element father, String name, ArrayList props) {
        Iterator i = props.iterator();
        while (i.hasNext()) {
            LinkedHashMap map = (LinkedHashMap) i.next();
            Element child = new Element(name);
            setChildElement(father, child, map);
        }
    }

    private void setChildElement(Element father, Element child, LinkedHashMap props) {
        Set key = props.keySet();
        Iterator it = key.iterator();

        while (it.hasNext()) {
            String name = (String) it.next();

            if (props.get(name) instanceof LinkedHashMap) {
                LinkedHashMap map = (LinkedHashMap) props.get(name);
                Element subChild = new Element(name);
                setChildElement(child, subChild, map);
            }
            else if (props.get(name) instanceof BigDecimal) {
                if (name.startsWith("ATTR:")) {
                    child.setAttribute(name.substring(5), props.get(name).toString());
                }
                else if (name.startsWith("VAL:")) {
                    child.setText(props.get(name).toString());
                }
                else {
                    setChildElement(child, name, props.get(name).toString());
                }
            }
            else if (props.get(name) instanceof String) {
                if (name.startsWith("ATTR:")) {
                    child.setAttribute(name.substring(5), (String) props.get(name));
                }
                else if (name.startsWith("VAL:")) {
                    child.setText((String) props.get(name));
                }
                else {
                    setChildElement(child, name, (String) props.get(name));
                }
            }
            else if (props.get(name) instanceof ArrayList) {
                setChildElement(child, name, (ArrayList) props.get(name));
            }
            else if (props.get(name) == null) {
                if (name.startsWith("ATTR:")) {
                    child.setAttribute(name.substring(5), "");
                }
                else {
                    setChildElement(child, name, "");
                }
            }
        }
        father.addContent(child);
    }

    private void setChildElement(Element father, String name, String value) {
        Element child = new Element(name);
        child.setText(value);
        father.addContent(child);
    }

    private void setChildElement(Element father, String name, CDATA value) {
        Element child = new Element(name);
        child.addContent(value);
        father.addContent(child);
    }
}
